<?php

namespace Biz\module\Behaviors\Order;

use Biz\model\activity\FullShipping as mFullShipping;
use Biz\module\Classes\Coupon as clsCoupon;
use Biz\module\Classes\Order;
use Biz\module\Classes\ShopCoupon as clsShopCoupon;
use Biz\module\Sys\ErrorConst;
use Biz\service\activity\FullShipping as sFullShipping;
use Biz\service\activity\MealInfo as sMealInfo;
use Biz\service\redPacket\RedPackets as sRedPackets;
use Biz\service\ShippingTempl;
use Biz\service\ShippingTempl as sShippingTempl;
use Biz\service\ShopInfo as sShopInfo;
use Biz\service\trade\Integral as sIntegral;
use Biz\tools\Amount as toolsAmount;
use Doraemon\tools\HhzClient;
use Doraemon\tools\HhzClient as tHhzClient;
use Doraemon\tools\Log as toolsLog;
use Doraemon\tools\Log;

class OrderFormat extends OrderFormatBase
{
    private $orderData = [
        "list" => [],
        "coupon" => null,
        "integral" => [
            'total' => 0,
            'usable' => 0,
            'discount' => 0,
            'text' => ''
        ],
        "summary" => [
            "goods_amount" => 0,
            "total_amount" => 0,
            "total_buynum" => 0
        ],
    ];

    public function computeConfirmOrder($uid, $list, $areaCode, $isToken = false, $sCouponInfo = [], $couponId = 0, $integral = 0, $mealId = 0, $selfMentionId = 0)
    {
        $appType = HhzClient::getClientType();

        if (!$list) return $this->orderData;

        //获取配置文件中平台允许的优惠类型
        $discountType = $this->getSkuDiscountType($appType);

        //处理购买商品信息
        if (!$isToken) {
            foreach ($list as $skuId => &$skuInfo) {
                if (!$this->skuNumList) {
                    $this->skuNumList[$skuInfo['sku_id']] = $skuInfo['buy_num'];
                }

                //确认sku的活动类型
                $activityType = $this->getSkuActivityType($mealId, $skuInfo['activity_type']);

                //套餐价格的格式化
                $this->_formatSkuInfo($skuInfo, $mealId);

                //读取配置,处理购买商品
                $this->useConfig($skuInfo, $activityType, $appType, $discountType);
            }
        } else {
            foreach ($list as $shopId => $shopOrder) {
                foreach ($shopOrder['sku_list'] as $skuInfo) {
                    $skuInfo['shop_id'] = $shopId;
                    $this->skuNumList[$skuInfo['sku_id']] = $skuInfo['buy_num'];

                    //确认sku的活动类型
                    $activityType = $this->getSkuActivityType($mealId, $skuInfo['activity_type']);

                    //套餐价格的格式化
                    $this->_formatSkuInfo($skuInfo, $mealId);

                    //读取配置,处理购买商品
                    $this->useConfig($skuInfo, $activityType, $appType, $discountType);

                    $list[$shopId]['sku_lit'] = $skuInfo;
                }
            }
        }

        //格式化订单信息
        $this->_formatOrderData($uid, $list, $areaCode, $isToken, $sCouponInfo, $couponId, $integral, $mealId, $selfMentionId);

        return $this->orderData;
    }

    private function _formatOrderData($uid, $list, $areaCode, $isToken = false, $sCouponInfo = [], $couponId = 0, $integral = 0, $mealId = 0, $selfMentionId = 0)
    {
        $allSkuIds = [];
        $goodsIds = [];
        $goodsTitles = []; //商品title
        $shopBuyNums = [];//店铺购买商品数量

        $shopGoodsAmount = [];//店铺商品goods_amount
        $shopCouponDiscountAmount = [];//店铺优惠券优惠金额
        $shopMealDiscountAmount = [];//店铺套餐优惠金额

        $shopCouponInfo = [];
        $shopMealInfo = [];

        if (empty($list)) return;

        //处理订单商品信息
        if (!$isToken) {
            //提取列表信息
            $shopList = [];
            foreach ($list as $skuId => $skuInfo) {
                $shopList[$skuInfo['shop_id']][$skuId] = $skuInfo;
                $allSkuIds[$skuInfo['shop_id']][] = $skuId;
                $goodsIds[] = $skuInfo['goods_id'];
                if (!isset($goodsTitles[$skuInfo['goods_id']])) {
                    $goodsTitles[$skuInfo['goods_id']] = isset($skuInfo['title']) ? $skuInfo['title'] : $skuInfo['goods_info']['title'];
                }
            }

            //获取店铺信息
            $shopIds = array_keys($shopList);
            $shopInfoList = sShopInfo::getShopInfoList($shopIds);

            //组装列表数据
            foreach ($shopList as $shopId => $skuList) {
                $shopInfo = $shopInfoList[$shopId];
                $this->orderData['list'][$shopId]['shop_info'] = [
                    'shop_id' => $shopInfo['shop_id'],
                    'shop_name' => $shopInfo['name'],
                    'imgurl' => $shopInfo['shop_imgurl'],
                    'shop_icon' => $shopInfo['shop_imgurl'],
                ];
                foreach ($skuList as $skuId => $skuInfo) {
                    $allSkuIds[$shopId][] = $skuId;
                    $this->orderData['list'][$shopId]['sku_list'][] = [
                        'sku_id' => $skuId,
                        'goods_id' => $skuInfo['goods_id'],
                        'title' => $skuInfo['goods_info']['title'],
                        'cover_img' => $skuInfo['cover_img'],
                        'spec_img' => $skuInfo['spec_img'],
                        'sku_desc' => $skuInfo['sku_desc'],
                        'ori_price' => $skuInfo['ori_price'],
                        'actual_price' => $skuInfo['actual_price'],
                        'buy_num' => $this->skuNumList[$skuId],
                        'actual_stock' => $skuInfo['actual_stock'],
                        'weight' => $skuInfo['weight'],
                        'volume' => $skuInfo['volume'],
                        'sku_tip' => null,
                        'activity_type' => $skuInfo['activity_type'],
                        'activity_id' => $skuInfo['activity_id'],
                        'activity_allowance' => (isset($skuInfo['activity_allowance']) && $skuInfo['activity_allowance']) ? $skuInfo['activity_allowance'] : 0,//平台活动补贴
                        'shipping_templ_id' => $skuInfo['goods_info']['shipping_templ_id'],
                        'commission' => $skuInfo['commission'],
                        'category_id' => $skuInfo['goods_info']['category_id'],
                        'second_category_id' => $skuInfo['goods_info']['second_category_id'],
                        'status' => $skuInfo['status'],
                        'goods_status' => $skuInfo['goods_info']['status'],
                        'shipping_info' => $skuInfo['goods_info']['shipping_info'],
                        'forecast_ship_time' => $skuInfo['forecast_ship_time'],
                        'ratio' => ($skuInfo['goods_info']['ratio'] >= 0) ? $skuInfo['goods_info']['ratio'] : null,
                        'ratio_ext' => $skuInfo['goods_info']['ratio_ext']
                    ];

                    $shopBuyNums[$shopId] = isset($shopBuyNums[$shopId]) ? $shopBuyNums[$shopId] : 0;
                    $shopBuyNums[$shopId] = bcadd($shopBuyNums[$shopId], $this->skuNumList[$skuId]);

                    $shopGoodsAmount[$shopId] = isset($shopGoodsAmount[$shopId]) ? $shopGoodsAmount[$shopId] : 0;
                    $shopGoodsAmount[$shopId] = bcadd($shopGoodsAmount[$shopId], $skuInfo['actual_price'] * $this->skuNumList[$skuId], 2);
                }
            }
        } else {
            foreach ($list as $shopId => $shopOrder) {
                $this->orderData['list'][$shopId]['shop_info'] = $shopOrder['shop_info'];
                foreach ($shopOrder['sku_list'] as $skuInfo) {
                    $skuId = $skuInfo['sku_id'];
                    $allSkuIds[$shopId][] = $skuId;
                    $goodsIds[] = $skuInfo['goods_id'];
                    if (!isset($goodsTitles[$skuInfo['goods_id']])) {
                        $goodsTitles[$skuInfo['goods_id']] = $skuInfo['title'];
                    }
                    $this->orderData['list'][$shopId]['sku_list'][] = $skuInfo;

                    $shopBuyNums[$shopId] = isset($shopBuyNums[$shopId]) ? $shopBuyNums[$shopId] : 0;
                    $shopBuyNums[$shopId] = bcadd($shopBuyNums[$shopId], $skuInfo['buy_num']);

                    $shopGoodsAmount[$shopId] = isset($shopGoodsAmount[$shopId]) ? $shopGoodsAmount[$shopId] : 0;
                    $shopGoodsAmount[$shopId] = bcadd($shopGoodsAmount[$shopId], $skuInfo['actual_price'] * $skuInfo['buy_num'], 2);
                }
            }
        }

        //店铺相关金额计算
        foreach ($allSkuIds as $shopId => $skuIds) {
            //店铺优惠券优惠金额
            $shopCouponDiscountAmount[$shopId] = 0;
            //店铺套餐优惠金额
            $shopMealDiscountAmount[$shopId] = 0;
            //店铺运费金额
            $shopShippingAmount[$shopId] = 0;

            //如果有套餐,套餐不能使用店铺优惠券
            if ($mealId) {
                //计算店铺套餐套餐优惠
                $mealGoodsPrice = $this->getShopMealAmount($mealId, $shopId);
                if ($mealGoodsPrice) {
                    $shopMealDiscountAmount[$shopId] = $mealGoodsPrice['detract_amount'];
                    $shopMealInfo[$shopId] = [
                        'meal_id' => $mealId,
                        'detract_amount' => $mealGoodsPrice['detract_amount']
                    ];
                    $this->orderData['list'][$shopId]['meal_sku'] = $mealGoodsPrice;
                } else {
                    toolsLog::order_Orderformat_warning('选择的套餐不存在或则信息错误', [
                        'uid' => $uid,
                        'meal_id' => $mealId,
                        'meal_goods' => $this->mealGoods[$shopId],
                    ]);
                    throw new \Exception(ErrorConst::$errorDesc[ErrorConst::ERROR_ORDER_CREATE_ORDER_DATA], ErrorConst::ERROR_ORDER_CREATE_ORDER_DATA);
                }
            } else if (!empty(HhzClient::getClientVersion()) && version_compare(HhzClient::getClientVersion(), '3.6.0', '>=')) {
                //计算店铺优惠券优惠
                $shopCouponData = $this->getShopCouponAmount($uid, $sCouponInfo, $shopId);
                $shopCouponInfo[$shopId] = $shopCouponData['coupon'];
                $shopCouponDiscountAmount[$shopId] = $shopCouponInfo[$shopId]['detract_amount'];
                $this->orderData['list'][$shopId]['coupon_sku'] = $shopCouponData['coupon_sku'];
            }


            //计算店铺运费
            $shopShippingAmount[$shopId] = $this->getShopShippingAmount($areaCode, $shopId, $selfMentionId);

            $this->orderData['list'][$shopId]['full_shipping_goods_ids'] = $shopShippingAmount[$shopId]['full_shipping_goods_ids'];
            $this->orderData['support_shipping'][$shopId] = $shopShippingAmount[$shopId]['support_shipping'];
            $this->orderData['sku_shipping_amount'][$shopId] = $shopShippingAmount[$shopId]['sku_shipping_amount'];


            //计算店铺购买数量，商品金额，运费,优惠信息
            $this->orderData['list'][$shopId]['summary']['total_buynum'] = (int)$shopBuyNums[$shopId];
            $this->orderData['list'][$shopId]['summary']['goods_amount'] = toolsAmount::format($shopGoodsAmount[$shopId]);
            $this->orderData['list'][$shopId]['summary']['shipping_fee'] = toolsAmount::format($shopShippingAmount[$shopId]['shipping_fee']);
            $this->orderData['list'][$shopId]['summary']['shipping_amount'] = toolsAmount::format($shopShippingAmount[$shopId]['shipping_amount']);
            $this->orderData['list'][$shopId]['summary']['full_shipping_plan_id'] = $shopShippingAmount[$shopId]['full_shipping_plan_id'];
            $this->orderData['list'][$shopId]['coupon'] = isset($shopCouponInfo[$shopId]) ? $shopCouponInfo[$shopId] : null;
            $this->orderData['list'][$shopId]['meal'] = isset($shopMealInfo[$shopId]) ? $shopMealInfo[$shopId] : null;

            //计算店铺小计
            $this->orderData['list'][$shopId]['summary']['total_amount'] = $this->orderData['list'][$shopId]['summary']['goods_amount'];
            $this->orderData['list'][$shopId]['summary']['total_amount'] = bcadd($this->orderData['list'][$shopId]['summary']['total_amount'], $shopShippingAmount[$shopId]['shipping_amount'], 2);
            $this->orderData['list'][$shopId]['summary']['total_amount'] = bcsub($this->orderData['list'][$shopId]['summary']['total_amount'], $shopCouponDiscountAmount[$shopId], 2);
            $this->orderData['list'][$shopId]['summary']['total_amount'] = bcsub($this->orderData['list'][$shopId]['summary']['total_amount'], $shopMealDiscountAmount[$shopId], 2);

            //订单总计
            if (!isset($this->orderData['summary']['total_buynum'])) {
                $this->orderData['summary']['total_buynum'] = 0;
            }
            if (!isset($this->orderData['summary']['total_amount'])) {
                $this->orderData['summary']['total_amount'] = 0;
            }
            if (!isset($this->orderData['summary']['shipping_fee'])) {
                $this->orderData['summary']['shipping_fee'] = 0;
            }
            if (!isset($this->orderData['summary']['shipping_amount'])) {
                $this->orderData['summary']['shipping_amount'] = 0;
            }
            if (!isset($this->orderData['summary']['goods_amount'])) {
                $this->orderData['summary']['goods_amount'] = 0;
            }
            $this->orderData['summary']['total_buynum'] = (int)$this->orderData['summary']['total_buynum'] + (int)$shopBuyNums[$shopId];
            $this->orderData['summary']['goods_amount'] = bcadd($this->orderData['summary']['goods_amount'], $this->orderData['list'][$shopId]['summary']['goods_amount'], 2);
            $this->orderData['summary']['shipping_fee'] = bcadd($this->orderData['summary']['shipping_fee'], $this->orderData['list'][$shopId]['summary']['shipping_fee'], 2);
            $this->orderData['summary']['shipping_amount'] = bcadd($this->orderData['summary']['shipping_amount'], $this->orderData['list'][$shopId]['summary']['shipping_amount'], 2);
            $this->orderData['summary']['total_amount'] = bcadd($this->orderData['summary']['total_amount'], $this->orderData['list'][$shopId]['summary']['total_amount'], 2);

            if (!isset($this->orderData['sku_ids'])) {
                $this->orderData['sku_ids'] = $skuIds;
            }
            $this->orderData['sku_ids'] = array_unique(array_merge($this->orderData['sku_ids'], $skuIds));
        }
        $this->orderData['goods_ids'] = array_unique($goodsIds);
        $this->orderData['goods_titles'] = $goodsTitles;
        //计算订单积分
        $integralInfo = [];
        $integralSku = [];

        //计算订单平台优惠券优惠
        $couponData = $this->getCouponAmount($uid, $couponId);
        $couponInfo = $couponData['coupon'];
        if ($couponInfo && $couponInfo['coupon_vid'] > 0 && $couponInfo['coupon_id'] > 0) {
            //优惠券和积分不能同时使用，有优惠券，则可用积分为0
            $integralInfo['usable'] = 0;
            $integralInfo['discount'] = 0;
            $integralInfo['text'] = '当前优惠券不支持与积分同享';
            $integralSku = [];
        } else {
            $integralData = $this->getIntegralAmount($uid);
            if ($integralData) {
                $integralInfo = $integralData['integral'];
                $integralSku = $integralData['integral_sku'];
            }
        }

        $this->orderData['red_packet'] = null;
        $redPacketData = $this->getRedPacketAmount($uid);
        if ($redPacketData && $redPacketData['discount'] > 0) {
            $this->orderData['red_packet'] = [
                'packet_amount' => $redPacketData['discount'],
                'text' => $redPacketData['msg'],
                'use_packet' => 1,
            ];
            $this->orderData['red_packet_sku'] = $redPacketData;
        }
        //($this->orderData['red_packet'],$this->orderData['red_packet_sku']);

        $this->orderData['integral'] = $integralInfo;
        $this->orderData['integral_sku'] = $integralSku;
        $this->orderData['coupon'] = $couponInfo;
        $this->orderData['coupon_sku'] = $couponData['coupon_sku'];
    }

    private function getShopCouponAmount($uid, $sCouponInfo, $shopId)
    {
        $shopCouponInfo = [];
        $shopCouponSkuInfo = [];
        if ($this->shopCouponGoods[$shopId]) {
            //最值优惠
            $bestShopCoupon = clsShopCoupon::getBestShopCouponSelect($uid, $shopId, $this->shopCouponGoods[$shopId]);
            if (!$bestShopCoupon || !$bestShopCoupon['coupon_vid'] || !$bestShopCoupon['coupon_id'] || !$bestShopCoupon['detract_amount']) {
                $bestShopCoupon = [];
            }
            if (is_null($sCouponInfo)) {
                if (!empty($bestShopCoupon)) {
                    $shopCouponDiscountArr = clsShopCoupon::calOrderSkuCouponDiscount($uid, $bestShopCoupon['coupon_id'], $bestShopCoupon['detract_amount'], '', $this->shopCouponGoods[$shopId]);
                    foreach ($shopCouponDiscountArr as $couponSkuId => $shopCouponDiscountRow) {
                        //积分，需要减掉店铺优惠券优惠金额
                        $this->integralGoods[$shopId][$couponSkuId]['amount'] = bcsub($this->integralGoods[$shopId][$couponSkuId]['amount'], $shopCouponDiscountRow['coupon_discount'], 2);
                        //平台券，需要减掉店铺优惠券优惠金额
                        $this->couponGoods[$couponSkuId]['total_amount'] = bcsub($this->couponGoods[$couponSkuId]['buy_num'] * $this->couponGoods[$couponSkuId]['buy_price'], $shopCouponDiscountRow['coupon_discount'], 2);
                        //红包，需要减掉店铺优惠券优惠金额
                        if ($this->redPacketGoods) {
                            $this->redPacketGoods[$couponSkuId]['amount'] = bcsub($this->redPacketGoods[$couponSkuId]['amount'], $shopCouponDiscountRow['coupon_discount'], 2);
                        }
                    }
                    $shopCouponSkuInfo = $shopCouponDiscountArr;
                    $shopCouponInfo = [
                        'coupon_vid' => $bestShopCoupon['coupon_vid'],
                        'coupon_id' => $bestShopCoupon['coupon_id'],
                        'title' => '最值优惠:-¥' . $bestShopCoupon['detract_amount'],
                        'detract_amount' => $bestShopCoupon['detract_amount']
                    ];
                } else {
                    $unUsedCoupons = clsShopCoupon::mineList($uid, clsShopCoupon::CATE_ID_UNUSED, ['shop_id' => $shopId]);
                    if ($unUsedCoupons) {
                        $shopCouponInfo = [
                            'coupon_vid' => -1,
                            'coupon_id' => -1,
                            'title' => '无可用优惠券',
                            'detract_amount' => 0
                        ];
                    }
                }
            } else {
                if (isset($sCouponInfo[$shopId])) {
                    if ($sCouponInfo[$shopId]['coupon_id'] != -1) {
                        //使用优惠券
                        $couponCanUse = clsShopCoupon::giveShopCouponIdCalCanUse($uid, $shopId, $sCouponInfo[$shopId]['coupon_id'], $this->shopCouponGoods[$shopId]);//判断优惠券是否能够使用
                        if (!$couponCanUse || !$couponCanUse['coupon_id'] || !$couponCanUse['coupon_vid'] || !$couponCanUse['detract_amount']) {
                            toolsLog::order_Orderformat_warning('选择的店铺优惠券不能使用', [
                                'uid' => $uid,
                                'shop_id' => $shopId,
                                'submit_coupon_id' => $sCouponInfo[$shopId]['coupon_id'],
                                'sku_buyList' => $this->shopCouponGoods[$shopId],
                                'coupon_can_use' => $couponCanUse,
                            ]);
                            throw new \Exception(ErrorConst::$errorDesc[ErrorConst::ERROR_ORDER_CREATE_ORDER_DATA], ErrorConst::ERROR_ORDER_CREATE_ORDER_DATA);
                        } else {
                            //店铺优惠券拿到后开始计算剩余的商品购买额
                            $shopCouponDiscountArr = clsShopCoupon::calOrderSkuCouponDiscount($uid, $couponCanUse['coupon_id'], $couponCanUse['detract_amount'], '', $this->shopCouponGoods[$shopId]);
                            foreach ($shopCouponDiscountArr as $couponSkuId => $shopCouponDiscountRow) {
                                //积分，需要减掉店铺优惠券优惠金额
                                $this->integralGoods[$shopId][$couponSkuId]['amount'] = bcsub($this->integralGoods[$shopId][$couponSkuId]['amount'], $shopCouponDiscountRow['coupon_discount'], 2);
                                //平台券，需要减掉店铺优惠券优惠金额
                                $this->couponGoods[$couponSkuId]['total_amount'] = bcsub($this->couponGoods[$couponSkuId]['buy_num'] * $this->couponGoods[$couponSkuId]['buy_price'], $shopCouponDiscountRow['coupon_discount'], 2);
                                //红包，需要减掉店铺优惠券优惠金额
                                if ($this->redPacketGoods) {
                                    $this->redPacketGoods[$couponSkuId]['amount'] = bcsub($this->redPacketGoods[$couponSkuId]['amount'], $shopCouponDiscountRow['coupon_discount'], 2);
                                }
                            }
                            $shopCouponSkuInfo = $shopCouponDiscountArr;
                            $shopCouponInfo = [
                                'coupon_vid' => $couponCanUse['coupon_vid'],
                                'coupon_id' => $couponCanUse['coupon_id'],
                                'title' => $couponCanUse['title'],
                                'detract_amount' => $couponCanUse['detract_amount']
                            ];
                            if ($bestShopCoupon && $bestShopCoupon['coupon_vid'] == $couponCanUse['coupon_vid']) {
                                $shopCouponInfo['title'] = '最值优惠:-¥' . $couponCanUse['detract_amount'];
                            }
                        }
                    } else {
                        //不使用优惠券
                        $shopCouponInfo = [
                            'coupon_vid' => -1,
                            'coupon_id' => -1,
                            'title' => '不使用优惠券',
                            'detract_amount' => 0
                        ];
                    }
                }
            }
        }
        $shopCouponData = ['coupon' => $shopCouponInfo, 'coupon_sku' => $shopCouponSkuInfo];
        return $shopCouponData;
    }

    private function getShopMealAmount($mealId, $shopId)
    {
        $mealGoodsPrice = [];
        if ($this->mealGoods[$shopId]) {
            $mealGoodsPrice = sMealInfo::getMealGoodsPrice($mealId, $this->mealGoods[$shopId]);
            foreach ($mealGoodsPrice['sku_price'] as $mealSkuId => $mealSkuRow) {
                //平台券，需要减掉套餐优惠
                $this->shopCouponGoods[$shopId][$mealSkuId]['total_amount'] = bcsub($this->shopCouponGoods[$shopId][$mealSkuId]['buy_num'] * $this->shopCouponGoods[$shopId][$mealSkuId]['buy_num'], $mealSkuRow['detract_price'], 2);
                //积分，需要减掉套餐优惠
                $this->integralGoods[$shopId][$mealSkuId]['amount'] = bcsub($this->integralGoods[$shopId][$mealSkuId]['amount'], $mealSkuRow['detract_price'], 2);
                //红包，需要减掉套餐优惠金额
                if ($this->redPacketGoods) {
                    $this->redPacketGoods[$mealSkuId]['amount'] = bcsub($this->redPacketGoods[$mealSkuId]['amount'], $mealSkuRow['detract_price'], 2);
                }
            }
        }
        return $mealGoodsPrice;
    }

    /**
     * @param string $areaCode 地区编码
     * @param int $shopId 店铺id
     * @param int $selfMentionId 自提店铺id
     * @return array
     * @throws \Exception
     */
    private function getShopShippingAmount($areaCode, $shopId, $selfMentionId = 0)
    {
        $supportShipping = ShippingTempl::checkGoodsSupportShipping($this->shippingGoods[$shopId], $areaCode);
        if ($selfMentionId > 0) {
            $gapFeeList = [];
            $shippingFee = 0;
            $skuPriceList = [];
        } else {
            $gapFeeList = sFullShipping::calGapFeeInfoInCart($areaCode, $this->fullShippingGoods);
            $shippingFeeArr = sShippingTempl::getShippingFee($shopId, $this->shippingGoods[$shopId], $areaCode);
            $shippingFee = $shippingFeeArr['price'];
            $skuPriceList = $shippingFeeArr['sku_price'];
        }

        $shippingAmount = $shippingFee;
        $fullShippingPlanId = 0;
        $fullShippingGoodsIds = [];
        if ($gapFeeList && isset($gapFeeList[$shopId]['type'])) {
            if ($gapFeeList[$shopId]['type'] == mFullShipping::TYPE_TOTAL_AMOUNT && $gapFeeList[$shopId]['gap_fee'] == 0) {
                $shippingAmount = 0;//满包邮
                $fullShippingPlanId = $gapFeeList[$shopId]['primary_id'];
                if (isset($gapFeeList[$shopId]['free_goods_ids']) && $gapFeeList[$shopId]['free_goods_ids']) {
                    foreach ($gapFeeList[$shopId]['free_goods_ids'] as $freeGoodsId) {
                        $fullShippingGoodsIds[] = $freeGoodsId;
                        //去掉满包邮的商品，计算非满包邮商品运费
                        unset($this->shippingGoods[$shopId][$freeGoodsId]);
                    }
                }
            } else if ($gapFeeList[$shopId]['type'] == mFullShipping::TYPE_TOTAL_PIECES && $gapFeeList[$shopId]['gap_pieces'] == 0) {
                $shippingAmount = 0;//满包邮
                $fullShippingPlanId = $gapFeeList[$shopId]['primary_id'];
                if (isset($gapFeeList[$shopId]['free_goods_ids']) && $gapFeeList[$shopId]['free_goods_ids']) {
                    foreach ($gapFeeList[$shopId]['free_goods_ids'] as $freeGoodsId) {
                        $fullShippingGoodsIds[] = $freeGoodsId;
                        //去掉满包邮的商品，计算非满包邮商品运费
                        unset($this->shippingGoods[$shopId][$freeGoodsId]);
                    }
                }
            }
            if ($this->shippingGoods && isset($this->shippingGoods[$shopId]) && $this->shippingGoods[$shopId]) {
                $shippingFeeArr = sShippingTempl::getShippingFee($shopId, $this->shippingGoods[$shopId], $areaCode);
                $shippingAmount = $shippingFeeArr['price'];
                $skuPriceList = $shippingFeeArr['sku_price'];
            }else{//不存在shippingGoods了，则运费为0。例如满包邮
                $skuPriceList = [];
            }
        }
        return ['support_shipping' => $supportShipping, 'shipping_fee' => $shippingFee, 'shipping_amount' => $shippingAmount, 'sku_shipping_amount' => $skuPriceList, 'full_shipping_plan_id' => $fullShippingPlanId, 'full_shipping_goods_ids' => $fullShippingGoodsIds];
    }

    private function getIntegralAmount($uid)
    {
        $integralData = [];
        $integralInfo = [];
        $userIntegral = sIntegral::getIntegral($uid);
        $integralInfo['total'] = $userIntegral;
        if ($this->integralGoods) {
            $canUseIntegral = sIntegral::calUseInOrder($userIntegral, $this->integralGoods);
            if ($canUseIntegral) {
                if (is_array($canUseIntegral)) {
                    $integralInfo['usable'] = $canUseIntegral['sum'];
                    $integralInfo['discount'] = $canUseIntegral['discount'];
                    $integralInfo['text'] = $canUseIntegral['msg'];
//                if ($canUseIntegral['sum'] <= 0 && $canUseIntegral['discount'] <= 0 && $this->limitRobGoods) {
//                    $integralInfo['text'] .= '(限时购商品不可使用积分)';
//                }
                    foreach ($canUseIntegral['data'] as $shopId => $skuList) {
                        foreach ($skuList['list'] as $skuId => $integral) {
                            $integralAmount = toolsAmount::format(($integral / sIntegral::CONVERSION_RATIO), 2);
                            //红包，需要减掉积分优惠金额
                            if ($this->redPacketGoods) {
                                $this->redPacketGoods[$skuId]['amount'] = bcsub($this->redPacketGoods[$skuId]['amount'], $integralAmount, 2);
                            }
                        }
                    }
                } else {
                    $integralInfo['text'] = $canUseIntegral;
                }
            }
            $integralData = ['integral' => $integralInfo, 'integral_sku' => $canUseIntegral];
        }
        return $integralData;
    }

    private function getRedPacketAmount($uid)
    {
        $redPacketData = [];
        if (version_compare(tHhzClient::getClientVersion(), '3.17.0', '>=')) {
            $packetAmount = sRedPackets::getCurrentAmount($uid);
            if ($this->redPacketGoods && $packetAmount) {
                $canUseRedPacket = sRedPackets::calUseInOrder($packetAmount, $this->redPacketGoods, $this->orderData['summary']['shipping_amount'] > 0 ? false : true);
                $redPacketData = $canUseRedPacket;
            }
        }
        return $redPacketData;
    }

    private function getCouponAmount($uid, $couponId)
    {
        $couponData = null;
        $couponInfo = [];
        $couponSkuInfo = [];
        if ($this->couponGoods) {
            $bestCoupon = clsCoupon::getBestCouponSelect($uid, $this->couponGoods);
            if (!$bestCoupon || !$bestCoupon['coupon_vid'] || !$bestCoupon['coupon_id'] || !$bestCoupon['detract_amount']) {
                $bestCoupon = [];
            }
            if (is_null($couponId)) {
                if ($bestCoupon) {
                    $couponDiscount = clsCoupon::calOrderSkuCouponDiscount($uid, $bestCoupon['coupon_id'], $bestCoupon['detract_amount'], '', $this->couponGoods);
                    foreach ($couponDiscount as $couponSkuId => $couponDiscountRow) {
                        //红包，需要减掉平台优惠券优惠金额
                        if ($this->redPacketGoods) {
                            $this->redPacketGoods[$couponSkuId]['amount'] = bcsub($this->redPacketGoods[$couponSkuId]['amount'], $couponDiscountRow['coupon_discount'], 2);
                        }
                    }
                    //使用优惠券，就不能使用积分
                    $couponInfo = [
                        'coupon_vid' => $bestCoupon['coupon_vid'],
                        'coupon_id' => $bestCoupon['coupon_id'],
                        'title' => '最值优惠:-¥' . $bestCoupon['detract_amount'],
                        'detract_amount' => $bestCoupon['detract_amount']
                    ];
                    $couponSkuInfo = $couponDiscount;
                } else {
                    $data['coupon'] = null;//不显示选择优惠券入口
                    $unUsedCoupons = clsCoupon::mineList($uid, clsCoupon::CATE_ID_UNUSED);
                    if ($unUsedCoupons) {
                        $couponInfo = [
                            'coupon_vid' => -1,
                            'coupon_id' => -1,
                            'title' => '无可用优惠券',
                            'detract_amount' => 0
                        ];
                    } else {
                        $couponInfo = [
                            'coupon_vid' => -1,
                            'coupon_id' => -1,
                            'title' => Order::zyjtVersion() ? '无可用优惠券' : '输入优惠码，兑换优惠券',
                            'detract_amount' => 0
                        ];
                    }
                }
            } else if ($couponId != -1) {
                //使用优惠券
                $couponCanUse = clsCoupon::giveCouponIdCalCanUse($uid, $couponId, '', $this->couponGoods);//判断优惠券是否能够使用
                if (!$couponCanUse || !$couponCanUse['coupon_id'] || !$couponCanUse['coupon_vid'] || !$couponCanUse['detract_amount']) {
                    toolsLog::order_Orderformat_warning('选择的平台优惠券不能使用', [
                        'uid' => $uid,
                        'submit_coupon_id' => $couponId,
                        'sku_buyList' => $this->couponGoods,
                        'coupon_can_use' => $couponCanUse,
                    ]);
                    throw new \Exception(ErrorConst::$errorDesc[ErrorConst::ERROR_ORDER_CREATE_ORDER_DATA], ErrorConst::ERROR_ORDER_CREATE_ORDER_DATA);
                } else {
                    $couponDiscount = clsCoupon::calOrderSkuCouponDiscount($uid, $couponCanUse['coupon_id'], $couponCanUse['detract_amount'], '', $this->couponGoods);
                    $couponSkuInfo = $couponDiscount;
                    $couponInfo = [
                        'coupon_vid' => $couponCanUse['coupon_vid'],
                        'coupon_id' => $couponCanUse['coupon_id'],
                        'title' => $couponCanUse['title'],
                        'type' => $couponCanUse['type'],
                        'discount_level' => $couponCanUse['discount_level'],
                        'detract_amount' => $couponCanUse['detract_amount']
                    ];
                    if ($bestCoupon && $bestCoupon['coupon_vid'] == $couponCanUse['coupon_vid']) {
                        $couponInfo['title'] = '最值优惠:-¥' . $couponCanUse['detract_amount'];
                    }
                }
            } else {
                //不使用优惠券
                $couponInfo = [
                    'coupon_vid' => -1,
                    'coupon_id' => -1,
                    'title' => '不使用优惠券',
                    'detract_amount' => 0
                ];
            }
        }
        $couponData = ['coupon' => $couponInfo, 'coupon_sku' => $couponSkuInfo];
        return $couponData;
    }

    private function _formatSkuInfo(&$skuInfo, $mealId)
    {
        if ($mealId) {
            //如果是套餐，没有别的活动
            $skuInfo['activity_type'] = null;
            $skuInfo['activity_id'] = null;
            // 购买价格,如果套餐，当前价用原价
            $skuInfo['actual_price'] = $skuInfo['ori_price'];
        }
    }


}